/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entityprojet;

import entityprojet.exceptions.IllegalOrphanException;
import entityprojet.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author T.Ron
 */
public class JournauxJpaController implements Serializable {

    public JournauxJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    
    public JournauxJpaController() {
    }
    
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("projetT_RPU");

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Journaux journaux) {
        if (journaux.getEcriturecCollection() == null) {
            journaux.setEcriturecCollection(new ArrayList<Ecriturec>());
        }
        if (journaux.getJoperiodeCollection() == null) {
            journaux.setJoperiodeCollection(new ArrayList<Joperiode>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Typejo typejo = journaux.getTypejo();
            if (typejo != null) {
                typejo = em.getReference(typejo.getClass(), typejo.getIdTypejo());
                journaux.setTypejo(typejo);
            }
            Collection<Ecriturec> attachedEcriturecCollection = new ArrayList<Ecriturec>();
            for (Ecriturec ecriturecCollectionEcriturecToAttach : journaux.getEcriturecCollection()) {
                ecriturecCollectionEcriturecToAttach = em.getReference(ecriturecCollectionEcriturecToAttach.getClass(), ecriturecCollectionEcriturecToAttach.getIdEc());
                attachedEcriturecCollection.add(ecriturecCollectionEcriturecToAttach);
            }
            journaux.setEcriturecCollection(attachedEcriturecCollection);
            Collection<Joperiode> attachedJoperiodeCollection = new ArrayList<Joperiode>();
            for (Joperiode joperiodeCollectionJoperiodeToAttach : journaux.getJoperiodeCollection()) {
                joperiodeCollectionJoperiodeToAttach = em.getReference(joperiodeCollectionJoperiodeToAttach.getClass(), joperiodeCollectionJoperiodeToAttach.getIdJop());
                attachedJoperiodeCollection.add(joperiodeCollectionJoperiodeToAttach);
            }
            journaux.setJoperiodeCollection(attachedJoperiodeCollection);
            em.persist(journaux);
            if (typejo != null) {
                typejo.getJournauxCollection().add(journaux);
                typejo = em.merge(typejo);
            }
            for (Ecriturec ecriturecCollectionEcriturec : journaux.getEcriturecCollection()) {
                Journaux oldNumjoOfEcriturecCollectionEcriturec = ecriturecCollectionEcriturec.getNumjo();
                ecriturecCollectionEcriturec.setNumjo(journaux);
                ecriturecCollectionEcriturec = em.merge(ecriturecCollectionEcriturec);
                if (oldNumjoOfEcriturecCollectionEcriturec != null) {
                    oldNumjoOfEcriturecCollectionEcriturec.getEcriturecCollection().remove(ecriturecCollectionEcriturec);
                    oldNumjoOfEcriturecCollectionEcriturec = em.merge(oldNumjoOfEcriturecCollectionEcriturec);
                }
            }
            for (Joperiode joperiodeCollectionJoperiode : journaux.getJoperiodeCollection()) {
                Journaux oldNumjoOfJoperiodeCollectionJoperiode = joperiodeCollectionJoperiode.getNumjo();
                joperiodeCollectionJoperiode.setNumjo(journaux);
                joperiodeCollectionJoperiode = em.merge(joperiodeCollectionJoperiode);
                if (oldNumjoOfJoperiodeCollectionJoperiode != null) {
                    oldNumjoOfJoperiodeCollectionJoperiode.getJoperiodeCollection().remove(joperiodeCollectionJoperiode);
                    oldNumjoOfJoperiodeCollectionJoperiode = em.merge(oldNumjoOfJoperiodeCollectionJoperiode);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Journaux journaux) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Journaux persistentJournaux = em.find(Journaux.class, journaux.getNumjo());
            Typejo typejoOld = persistentJournaux.getTypejo();
            Typejo typejoNew = journaux.getTypejo();
            Collection<Ecriturec> ecriturecCollectionOld = persistentJournaux.getEcriturecCollection();
            Collection<Ecriturec> ecriturecCollectionNew = journaux.getEcriturecCollection();
            Collection<Joperiode> joperiodeCollectionOld = persistentJournaux.getJoperiodeCollection();
            Collection<Joperiode> joperiodeCollectionNew = journaux.getJoperiodeCollection();
            List<String> illegalOrphanMessages = null;
            for (Ecriturec ecriturecCollectionOldEcriturec : ecriturecCollectionOld) {
                if (!ecriturecCollectionNew.contains(ecriturecCollectionOldEcriturec)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Ecriturec " + ecriturecCollectionOldEcriturec + " since its numjo field is not nullable.");
                }
            }
            for (Joperiode joperiodeCollectionOldJoperiode : joperiodeCollectionOld) {
                if (!joperiodeCollectionNew.contains(joperiodeCollectionOldJoperiode)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Joperiode " + joperiodeCollectionOldJoperiode + " since its numjo field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (typejoNew != null) {
                typejoNew = em.getReference(typejoNew.getClass(), typejoNew.getIdTypejo());
                journaux.setTypejo(typejoNew);
            }
            Collection<Ecriturec> attachedEcriturecCollectionNew = new ArrayList<Ecriturec>();
            for (Ecriturec ecriturecCollectionNewEcriturecToAttach : ecriturecCollectionNew) {
                ecriturecCollectionNewEcriturecToAttach = em.getReference(ecriturecCollectionNewEcriturecToAttach.getClass(), ecriturecCollectionNewEcriturecToAttach.getIdEc());
                attachedEcriturecCollectionNew.add(ecriturecCollectionNewEcriturecToAttach);
            }
            ecriturecCollectionNew = attachedEcriturecCollectionNew;
            journaux.setEcriturecCollection(ecriturecCollectionNew);
            Collection<Joperiode> attachedJoperiodeCollectionNew = new ArrayList<Joperiode>();
            for (Joperiode joperiodeCollectionNewJoperiodeToAttach : joperiodeCollectionNew) {
                joperiodeCollectionNewJoperiodeToAttach = em.getReference(joperiodeCollectionNewJoperiodeToAttach.getClass(), joperiodeCollectionNewJoperiodeToAttach.getIdJop());
                attachedJoperiodeCollectionNew.add(joperiodeCollectionNewJoperiodeToAttach);
            }
            joperiodeCollectionNew = attachedJoperiodeCollectionNew;
            journaux.setJoperiodeCollection(joperiodeCollectionNew);
            journaux = em.merge(journaux);
            if (typejoOld != null && !typejoOld.equals(typejoNew)) {
                typejoOld.getJournauxCollection().remove(journaux);
                typejoOld = em.merge(typejoOld);
            }
            if (typejoNew != null && !typejoNew.equals(typejoOld)) {
                typejoNew.getJournauxCollection().add(journaux);
                typejoNew = em.merge(typejoNew);
            }
            for (Ecriturec ecriturecCollectionNewEcriturec : ecriturecCollectionNew) {
                if (!ecriturecCollectionOld.contains(ecriturecCollectionNewEcriturec)) {
                    Journaux oldNumjoOfEcriturecCollectionNewEcriturec = ecriturecCollectionNewEcriturec.getNumjo();
                    ecriturecCollectionNewEcriturec.setNumjo(journaux);
                    ecriturecCollectionNewEcriturec = em.merge(ecriturecCollectionNewEcriturec);
                    if (oldNumjoOfEcriturecCollectionNewEcriturec != null && !oldNumjoOfEcriturecCollectionNewEcriturec.equals(journaux)) {
                        oldNumjoOfEcriturecCollectionNewEcriturec.getEcriturecCollection().remove(ecriturecCollectionNewEcriturec);
                        oldNumjoOfEcriturecCollectionNewEcriturec = em.merge(oldNumjoOfEcriturecCollectionNewEcriturec);
                    }
                }
            }
            for (Joperiode joperiodeCollectionNewJoperiode : joperiodeCollectionNew) {
                if (!joperiodeCollectionOld.contains(joperiodeCollectionNewJoperiode)) {
                    Journaux oldNumjoOfJoperiodeCollectionNewJoperiode = joperiodeCollectionNewJoperiode.getNumjo();
                    joperiodeCollectionNewJoperiode.setNumjo(journaux);
                    joperiodeCollectionNewJoperiode = em.merge(joperiodeCollectionNewJoperiode);
                    if (oldNumjoOfJoperiodeCollectionNewJoperiode != null && !oldNumjoOfJoperiodeCollectionNewJoperiode.equals(journaux)) {
                        oldNumjoOfJoperiodeCollectionNewJoperiode.getJoperiodeCollection().remove(joperiodeCollectionNewJoperiode);
                        oldNumjoOfJoperiodeCollectionNewJoperiode = em.merge(oldNumjoOfJoperiodeCollectionNewJoperiode);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = journaux.getNumjo();
                if (findJournaux(id) == null) {
                    throw new NonexistentEntityException("The journaux with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Journaux journaux;
            try {
                journaux = em.getReference(Journaux.class, id);
                journaux.getNumjo();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The journaux with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Ecriturec> ecriturecCollectionOrphanCheck = journaux.getEcriturecCollection();
            for (Ecriturec ecriturecCollectionOrphanCheckEcriturec : ecriturecCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Journaux (" + journaux + ") cannot be destroyed since the Ecriturec " + ecriturecCollectionOrphanCheckEcriturec + " in its ecriturecCollection field has a non-nullable numjo field.");
            }
            Collection<Joperiode> joperiodeCollectionOrphanCheck = journaux.getJoperiodeCollection();
            for (Joperiode joperiodeCollectionOrphanCheckJoperiode : joperiodeCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Journaux (" + journaux + ") cannot be destroyed since the Joperiode " + joperiodeCollectionOrphanCheckJoperiode + " in its joperiodeCollection field has a non-nullable numjo field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Typejo typejo = journaux.getTypejo();
            if (typejo != null) {
                typejo.getJournauxCollection().remove(journaux);
                typejo = em.merge(typejo);
            }
            em.remove(journaux);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Journaux> findJournauxEntities() {
        return findJournauxEntities(true, -1, -1);
    }

    public List<Journaux> findJournauxEntities(int maxResults, int firstResult) {
        return findJournauxEntities(false, maxResults, firstResult);
    }

    private List<Journaux> findJournauxEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Journaux.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Journaux findJournaux(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Journaux.class, id);
        } finally {
            em.close();
        }
    }

    public int getJournauxCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Journaux> rt = cq.from(Journaux.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
